﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1stVATCWin
{
    public partial class SP_ADD : Form
    {
        public string tenanh;
        public SP_ADD()
        {
            InitializeComponent();
        }

        private void SP_ADD_Load(object sender, EventArgs e)
        {
            load_loaisp();
        }

        private void load_loaisp()
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from LOAISANPHAM";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Table LOAISANPHAM");
            DataTable dt = ds.Tables["Table LOAISANPHAM"];

            loaisp.DisplayMember = "tenloai";
            loaisp.ValueMember = "IDloai";
            loaisp.DataSource = ds.Tables["Table LOAISANPHAM"].DefaultView;
        }

        private void loadanhsp_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            if (f.ShowDialog() == DialogResult.OK)
            {
                string filename = f.FileName;
                tenanh = System.IO.Path.GetFileName(f.FileName);
                pictureBox1.Image = Image.FromFile(f.FileName);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            //SaveFileDialog s = new SaveFileDialog();
            //s.InitialDirectory = @"C:\Users\FOX\Desktop\1stwed\1stVATCWed\Img";
            //s.FileName = tenanh;
            //if (pictureBox1.Image != null && s.ShowDialog() == DialogResult.OK)
            //{               
                Config file = new Config(Application.StartupPath + "\\config.ini");
                string connectionString = file.ReadValue("server", "conn");
                SqlConnection conn = new SqlConnection(connectionString);
                string sql = "insert into SANPHAM (IDsanpham,tensanpham,hinhanh,ngay,giaban,hanbaohanh,donvitinh,soluongton,IDloai) values ('0',@tsp,@tenanh,@ngaysp,0,@hbh,'CAI',0,@lsp)";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@tsp", tsp.Text);
                cmd.Parameters.AddWithValue("@tenanh", tenanh);
                cmd.Parameters.AddWithValue("@ngaysp", ngaysp.Value);
                cmd.Parameters.AddWithValue("@hbh", hbh.Text);
                cmd.Parameters.AddWithValue("@lsp", loaisp.SelectedValue);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

            //    System.IO.FileStream fs = (System.IO.FileStream)s.OpenFile();
            //    pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
            //    fs.Close();

            //}            
        }
    }
}
